﻿// ***********************************************************************
// Assembly         : STX.Lib
// Author           : seven.x
// Created          : 12-27-2013
//
// Last Modified By : seven.x
// Last Modified On : 12-27-2013
// ***********************************************************************
// <copyright file="STXConfig.cs" company="驰创数码">
//     Copyright (c) 驰创数码. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Xml.XPath;

namespace STX.Lib.Utils
{
    /// <summary>
    /// Class STXConfig.
    /// </summary>
    public class STXConfig
    {
        /// <summary>
        /// The configuration XML
        /// </summary>
        static readonly XDocument ConfigXml;
        /// <summary>
        /// Initializes static members of the <see cref="STXConfig"/> class.
        /// </summary>
        static STXConfig()
        {
            ConfigXml = XDocument.Load(Common.MapPath("/STX.config"));
        }

        /// <summary>
        /// Gets the value.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns>System.String.</returns>
        public static string GetValue(string path)
        {
            var elm = ConfigXml.XPathSelectElement(path);
            return elm != null ? elm.Value : "";
        }

        /// <summary>
        /// Gets the executable element.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns>XElement.</returns>
        public static XElement GetXElement(string path)
        {
            return ConfigXml.XPathSelectElement(path);
        }

        /// <summary>
        /// Gets the executable elements.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns>IEnumerable{XElement}.</returns>
        public static IEnumerable<XElement> GetXElements(string path)
        {
            return ConfigXml.XPathSelectElements(path);
        }

        /// <summary>
        /// Gets the current execute path.
        /// </summary>
        /// <value>The current execute path.</value>
        public static string CurrentExecutePath
        {
            get
            {
                var path = "";
                if (Environment.CurrentDirectory == AppDomain.CurrentDomain.BaseDirectory)//Windows应用程序则相等
                {
                    path = AppDomain.CurrentDomain.BaseDirectory;
                }
                else
                {
                    path = AppDomain.CurrentDomain.BaseDirectory + @"Bin\";
                }
                return path;
            }
        }
    }
}
